﻿using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using CHFramework;
using System;

public class UITopMainPanel : BaseUI 
{
    public static UITopMainPanel Instance;

    public Text CoinLabel;
    public Text BambooLabel;

    private void Awake()
    {
        Instance = this;

        base.CurrentUIType.PanelType = UIPanelType.Fixed;
        base.CurrentUIType.ShowMode = UIPanelShowMode.Normal;

        UpdateUI();
    }

    public void UpdateUI()
    {
        CoinLabel.text = UsersDataManager.Instance.UserData.Coin.ToString();
        BambooLabel.text = UsersDataManager.Instance.UserData.Bamboo.ToString();
    }

    public void UpdateCoin(EventData evt)
    {
        if (evt.EventParam != null)
        {
            int coin = Convert.ToInt16(evt.EventParam.ToString());
            UsersDataManager.Instance.UserData.Coin += coin;
            CoinLabel.text = UsersDataManager.Instance.UserData.Coin.ToString();
        }
    }

    public void UpdateCash(EventData evt)
    {
        if (evt.EventParam != null)
        {
            int bamboo = Convert.ToInt16(evt.EventParam.ToString());
            UsersDataManager.Instance.UserData.Bamboo += bamboo;
            BambooLabel.text = UsersDataManager.Instance.UserData.Bamboo.ToString();
        }
    }

    private void Destroy()
    {
        EventManager.Instance.RemoveEventListener(SysDefine.UpdateCoin, UpdateCoin);
        EventManager.Instance.RemoveEventListener(SysDefine.UpdateCash, UpdateCash);
    }
}
